/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved.
 */
import path from 'path';

import { expect } from '@jest/globals';
import fsExtra from 'fs-extra';

import { initExecuteCommand, runHvigorAsync } from '../../common/common';
import {
  AbilityType,
  APIType,
  APIVersion,
  CaseLevel,
  IsEnable,
  LanguageType,
  ModuleTemplateType,
  RuntimeOSType,
  SDKType,
  SignType,
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOption } from '../../plugins/task-builder/test-task-option';

const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API10,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility,
  },

  modulesOption: {
    [ModuleTemplateType.SharedLibrary]: 1,
  },
};

const projectBuilderOption11: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,
    apiVersion: APIVersion.API11,
    apiType: APIType.STAGE,
    language: LanguageType.ArkTs,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.EmptyAbility,
  },

  modulesOption: {
    [ModuleTemplateType.SharedLibrary]: 1,
  },
};

const taskConfig: TestTaskOption = {
  caseLevel: CaseLevel.L1,
  sign: SignType.NoSign,
  ohpmInstall: IsEnable.DisAble,
};

const testFun: TestCaseFun = async (project, taskConfig, record) => {
  // 测试工程主目录路径
  const projectRoot = project.projectRootPath();

  // 需要执行的命令行参数
  const commands = initExecuteCommand({
    mode: 'module',
    product: 'default',
    tasks: 'assembleHsp',
  });

  // 在shareLibrary下创建changelog.md、readme.md、license文件
  const str = '';
  await fsExtra.writeFile(path.resolve(project.getModule('shareLibrary').moduleRootPath(), 'changelog.md'), str);
  await fsExtra.writeFile(path.resolve(project.getModule('shareLibrary').moduleRootPath(), 'changelog-EN.md'), str);
  await fsExtra.writeFile(path.resolve(project.getModule('shareLibrary').moduleRootPath(), 'readme.md'), str);
  await fsExtra.writeFile(path.resolve(project.getModule('shareLibrary').moduleRootPath(), 'readme-EN.md'), str);
  await fsExtra.writeFile(path.resolve(project.getModule('shareLibrary').moduleRootPath(), 'license'), str);

  // 判断是否在shareLibrary下创建changelog.md、readme.md、license文件
  const existE2EFile = await fsExtra.pathExists(path.resolve(project.getModule('shareLibrary').moduleRootPath(), 'changelog.md'));
  const existE2EFile3 = await fsExtra.pathExists(path.resolve(project.getModule('shareLibrary').moduleRootPath(), 'changelog-EN.md'));
  const existE2EFile1 = await fsExtra.pathExists(path.resolve(project.getModule('shareLibrary').moduleRootPath(), 'readme.md'));
  const existE2EFile4 = await fsExtra.pathExists(path.resolve(project.getModule('shareLibrary').moduleRootPath(), 'readme-EN.md'));
  const existE2EFile2 = await fsExtra.pathExists(path.resolve(project.getModule('shareLibrary').moduleRootPath(), 'license'));
  expect(existE2EFile).toBe(true);
  expect(existE2EFile1).toBe(true);
  expect(existE2EFile2).toBe(true);
  expect(existE2EFile3).toBe(true);
  expect(existE2EFile4).toBe(true);

  // 执行编译
  const result = await runHvigorAsync(projectRoot, commands);
  record.addStep('执行hsp编译构建', result);
  expect(result.stdout).toContain('BUILD SUCCESSFUL');
};

// 将demo测试用例注册到测试用例中心
TestCaseBuilder.builder()
  .caseName('OPEN_LAB_AIDE_ZOS_HOS_STAGE_Shared_Library_api10_034')
  .description('hsp模块将文件打入.har产物中')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(taskConfig)
  .testFun(testFun)
  .load();

TestCaseBuilder.builder()
  .caseName('OPEN_LAB_AIDE_ZOS_HOS_STAGE_Shared_Library_api11_034')
  .description('hsp模块将文件打入.har产物中')
  .projectBuilderOption(projectBuilderOption11)
  .taskConfig(taskConfig)
  .testFun(testFun)
  .load();
